package com.community.controller;

import com.community.pojo.ElderlyHealth;
import com.community.pojo.User;
import com.community.service.UserElderlyHealthService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserElderlyHealthController {
    private final UserElderlyHealthService userElderlyHealthService;
    @Autowired
    public UserElderlyHealthController(UserElderlyHealthService userElderlyHealthService) {
        this.userElderlyHealthService = userElderlyHealthService;
    }

    @GetMapping("/find-user-health-information")
    public String findUserHealthInformationController(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        ElderlyHealth elderlyHealth = userElderlyHealthService.findUserHealthInformation(userId);
        if (elderlyHealth == null) {
            model.addAttribute("message", "该用户的健康信息还未录入！");
            return "findUserHealthInformationResult";
        }
        model.addAttribute("elderlyHealth", elderlyHealth);
        return "findUserHealthInformationResult";
    }

    @GetMapping("/update-user-health-information-form")
   public String updateUserHealthInformationFormController(HttpSession session, Model model) {
        User User = (User) session.getAttribute("user");
        ElderlyHealth elderlyHealth = userElderlyHealthService.findUserHealthInformation(User.getUserId());
        model.addAttribute("elderlyHealth", elderlyHealth);
        return "updateUserHealthInformationForm";
    }
    @PostMapping("/update-user-health-information")
    public String updateUserHealthInformationController(@ModelAttribute ElderlyHealth elderlyHealth,Model model) {
        int n= userElderlyHealthService.updateUserHealthInofrmation(elderlyHealth);
        if(n>0){
            model.addAttribute("message", "修改成功！");
        }else{
            model.addAttribute("message", "修改失败！");
        }
        return "updateUserHealthInformationResult";
    }

    @GetMapping("/add-user-health-information-form")
    public String addUserHealthInformationFormController(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        ElderlyHealth elderlyHealth = userElderlyHealthService.findUserHealthInformation(user.getUserId());
        if (elderlyHealth != null) {
            model.addAttribute("message", "健康信息已经存在，无法再次添加！");
            return "userMainPage";
        }
       return "addUserHealthInformationForm";
    }
    @PostMapping("/add-user-health-information")
    public String addUserHealthInformationController(@ModelAttribute ElderlyHealth elderlyHealth,HttpSession session,Model model) {
        User user = (User) session.getAttribute("user");
        elderlyHealth.setUserId(user.getUserId());
        if (elderlyHealth.getHeight() == null ||
                elderlyHealth.getWeight() == null ||
                elderlyHealth.getAge() == null ||
                elderlyHealth.getHealthCondition() == null ||
                elderlyHealth.getHealthCondition().trim().equals("")) {
            model.addAttribute("message", "不允许出现空值！");
            return "addUserHealthInformationForm";
        }
        int n= userElderlyHealthService.insertUserHealthInformation(elderlyHealth);
        if(n>0){
            model.addAttribute("message", "添加成功，新添加的的健康id为: " + elderlyHealth.getHealthId());
        }else{
            model.addAttribute("message", "添加失败！");
        }
        return "userMainPage";
    }
}
